#include <p18f46k22.h>
#include "transmission.h"

#pragma code

void initTransmission(void){
	char donnee[7] = {'T','a',' ','m','e','r','e'};
	//Initialise les ux_DATAx en sortie
	TRISAbits.RA0 = 0;
	TRISAbits.RA1 = 0;
	TRISAbits.RA2 = 0;
	TRISAbits.RA3 = 0;
	
	TRISAbits.RA4 = 0;
	TRISAbits.RA5 = 1;
	
	TRISDbits.RD4 = 0;
	TRISDbits.RD5 = 0;
	TRISDbits.RD6 = 0;
	TRISDbits.RD7 = 0;
	//Envoie un message de test
	envoyerDonnees(donnee,7);
}

char* convertirDonnees(int X, int Y, int Z){
	char donnee[17] ={'X','=','0','0','0',' ','Y','=','0','0','0',' ','Z','=','0','0','0'};
	donnee[2]=X%10;
	donnee[3]=X%100;
	donnee[4]=X%1000;
	donnee[8]=Y%10;
	donnee[9]=Y%100;
	donnee[10]=Y%1000;
	donnee[14]=Z%10;
	donnee[15]=Z%100;
	donnee[16]=Z%1000;
	return donnee;
}

void envoyerDonnees(char donnee[], int longueur){
	int i;
	unsigned char envoye;
	LATAbits.LATA4 = 0;
	for(i=0;i<longueur;i++){
		LATAbits.LATA4 = 1;
		while(PORTAbits.RA5!=1);
		envoye = donnee[i];
		LATAbits.LATA0 = envoye&0x01;
		LATAbits.LATA1 = (envoye&0x02)>>1;
		LATAbits.LATA2 = (envoye&0x04)>>2;
		LATAbits.LATA3 = (envoye&0x08)>>3;
		LATDbits.LATD4 = (envoye&0x10)>>4;
		LATDbits.LATD5 = (envoye&0x20)>>5;
		LATDbits.LATD6 = (envoye&0x40)>>6;
		LATDbits.LATD7 = (envoye&0x80)>>7;
		LATAbits.LATA4 = 0;
		while(PORTAbits.RA5!=0);
	}
}